﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace BuildYahooURI
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            WebClient wc = new WebClient();
            Uri uri = new Uri("http://localhost:88/ws/Sample/BuildYahooURI/GetPriceHistory.aspx?ticker=MSFT&startdate=1-7-2009&enddate=1-10-2009"
                , UriKind.RelativeOrAbsolute);
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(uri); 
        } 
        
        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            StreamReader read = new StreamReader(e.Result);
            string strXml = read.ReadToEnd();
            XDocument xmlDoc = XDocument.Parse(strXml);
            IEnumerable<TimeSeriesData> myTimeSeries = from item in xmlDoc.Descendants("TimeSeries")
                where item.Element("Open") != null 
                select new TimeSeriesData
                {
                    open = Convert.ToDouble(item.Element("Open").Value
                    , new CultureInfo("en-US").NumberFormat)
                };
            
            Dispatcher.BeginInvoke(() => _items.ItemsSource = myTimeSeries);
        }
    }
}


public class TimeSeriesData
{
    public DateTime date { get; set; }
    public double open { get; set; }
    public double close { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double volume { get; set; }
    public double adjclose { get; set; }
    
    public TimeSeriesData(DateTime dteIn, double openIn, double closeIn, double highIn, double lowIn, double volumeIn, double adjCloseIn)
    {
        date = dteIn;
        open = openIn;
        close = closeIn;
        high = highIn;
        low = lowIn;
        volume = volumeIn;
        adjclose = adjCloseIn;
    }
    
    public TimeSeriesData() { }
}
